package jtreechart;

import bms.model.Battery;
import java.awt.BorderLayout;
import java.awt.Dimension;

import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultValueDataset;

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class XYChartExample extends JFrame {

    static class DemoPanel extends JPanel
            implements ChangeListener {

        DefaultValueDataset dataset1;
        DefaultValueDataset dataset2;
        JSlider slider1;
        JSlider slider2;

        public void stateChanged(ChangeEvent changeevent) {
            dataset1.setValue(new Integer(slider1.getValue()));
            dataset2.setValue(new Integer(slider2.getValue()));
        }

        public DemoPanel() {
            super(new BorderLayout());

            /*Create a simple Bar chart
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            dataset.setValue(6, "temp", "Jane");
            dataset.setValue(11.2, "volts", "Jane");
            
            dataset.setValue(70, "temp", "Tom");
            dataset.setValue(17.2, "volts", "Tom");
            
            dataset.setValue(48, "temp", "Jill");
            dataset.setValue(8.5, "volts", "Jill");
            
            dataset.setValue(25, "temp", "John");
            dataset.setValue(5.5, "volts", "John");
            
            dataset.setValue(12, "temp", "Fred");
            dataset.setValue(1.3, "volts", "Fred");
            
            
            
            JFreeChart chart = ChartFactory.createBarChart(
            "Comparison between Salesman",
            "Salesman",
            "Profit",
            dataset, PlotOrientation.VERTICAL,
            true,
            true,
            false);
            chart.setTitle("Dial Demo 2");
             */
            Battery b = new Battery();
            List<Battery> dataTemp = b.returnData();
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();


            /*
            XYSeries series = new XYSeries("XYGraph");
            series.getAllowDuplicateXValues();
             */
            for (Battery searchable : dataTemp) {
                dataset.setValue(searchable.getVoltage(), "temp", searchable.getBatteryNumber());
                //series.add(Integer.parseInt(searchable.getBatteryNumber()), searchable.getVoltage());
            }

            /* Add the series to your data set
            XYSeriesCollection dataset = new XYSeriesCollection();
            dataset.addSeries(series);
             */
// Generate the graph
            JFreeChart chart = ChartFactory.createBarChart(
                    "XY Chart", // Title
                    "x-axis", // x-axis Label
                    "y-axis", // y-axis Label
                    dataset, // Dataset
                    PlotOrientation.VERTICAL, // Plot Orientation
                    true, // Show Legend
                    true, // Use tooltips
                    true // Configure chart to generate URLs?
                    );

            ChartPanel chartpanel = new ChartPanel(chart, true, true, true, true, false);
            chartpanel.setPreferredSize(new Dimension(1200, 600));

            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            

            add(chartpanel);

        }
    }

    public XYChartExample(String s) {
        super(s);
        setDefaultCloseOperation(3);
        setContentPane(createDemoPanel());
    }

    public static JPanel createDemoPanel() {
        return new DemoPanel();
    }

    public static void main(String args[]) {

        XYChartExample xy = new XYChartExample("Battery Meter System");
        xy.pack();
        xy.setVisible(true);




    }
}